package com.yunao.service.report;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String HOUR_FORMAT = "yyyy-MM-dd HH";
	public static final String MINUTE_FORMAT = "yyyy-MM-dd HH:mm";
	public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String ESYTIME_FORMAT = "M-d";
	public static String long2Str(long l, String format) {
		return date2Str(new Date(l), format);
	}

	public static String date2Str(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
	public static Date getDate(String s) {
		Date date = null;
		if (s == null)
			return new Date();
		try {
			if (s.length() > 10) {
				date = new SimpleDateFormat(TIME_FORMAT).parse(s);
			} else {
				date = new SimpleDateFormat(DATE_FORMAT).parse(s);
			}
		} catch (Exception e) {

		}
		return date;
	}
	public static long[] getSearchTime(String period) {
		if (StringUtils.isEmpty(period))
			period = "3";

		long[] time = new long[2];

		if (period.indexOf(",") > 0) {
			DateTimeFormatter formatter = DateTimeFormat
					.forPattern(TIME_FORMAT);

			DateTime before = DateTime.parse(
					StringUtils.substringBefore(period, ","),formatter);
			DateTime end = DateTime.parse(
					StringUtils.substringAfter(period, ","), formatter);

			if (before.isAfter(end)) {
				DateTime temp = before;
				before = end;
				end = temp;
			}

			time[0] = before.getMillis();
			time[1] = end.getMillis();
		} else {
			time[0] = new DateTime().minusDays(Convert.getInt(period))
					.millisOfDay().withMinimumValue().getMillis();
			time[1] = new DateTime().millisOfDay().withMaximumValue()
					.getMillis();
		}
		return time;
	}

	public static Date getDate(String s, String dateFormat) {
		Date date = null;
		if (s == null)
			return null;
		try {
			date = new SimpleDateFormat(dateFormat).parse(s);
		} catch (Exception e) {

		}
		return date;
	}

	/**
	 * 获得时间点已清零的 long 时间
	 */
	public static long getClearedTime(int offset) {
		return getClearedTime(null, offset);
	}

	/**
	 * 获得时间点已清零的 long 时间
	 */
	public static long getClearedTime(String s, int offset) {
		Date date = getDate(s);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.set(Calendar.DATE, c.get(Calendar.DATE) - offset);
		return c.getTimeInMillis();
	}

	/**
	 * 获得String型当前时间
	 */
	public static String getCurrDate(String dateFormat) {
		return new SimpleDateFormat(dateFormat).format(new Date());
	}


    public  static Date getStartTime() {  
        Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR, -12);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 1);  
        todayStart.set(Calendar.MILLISECOND, 0);  
        return todayStart.getTime();  
    }  
  
    public static Date getEndTime() {  
        Calendar todayEnd = Calendar.getInstance();  
        todayEnd.set(Calendar.HOUR, 12);  
        todayEnd.set(Calendar.MINUTE, 0);  
        todayEnd.set(Calendar.SECOND, -1);  
        todayEnd.set(Calendar.MILLISECOND, 999);  
        return todayEnd.getTime();  
    }

}
